<template>
    <el-dialog :visible.sync="showAddModal" :append-to-body="true"
               :close-on-click-modal="false" width="40%"
               :close-on-press-escape="false" :show-close="false" class="ele-modal">
        <h4 slot="title">{{modalLabel}}</h4>
        <el-form :model="newForm" class="demo-form-inline" :rules="rules" ref="ruleForm">
            <el-row>
                <el-col :span="16">
                    <el-form-item label="角色名称" :label-width="formLabelWidth" prop="roleName">
                        <el-input v-model="newForm.roleName" auto-complete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="16">
                    <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
                        <el-input v-model="newForm.remark" auto-complete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="row" style="text-align: center">
            <a class="revision-btn" href="javaScript:void(0)" @click="onSubmit">保 存</a>
            <a class="revision-btn-default" style="margin-left: 5px;" href="javaScript:void(0)" @click="onCancel">取
                消</a>
        </div>
    </el-dialog>
</template>

<script>

    export default {
        name: "addRoleModal",
        props: {
            showAddModal: {
                type: Boolean,
                default: false
            },
            editId: {
                type: String,
                default: ''
            }
        },
        data() {
            return {
                modalLabel: '',
                newForm: {
                    roleId: '',
                    roleName: '',
                    remark: '',
                    menuIdList: []
                },
                formLabelWidth: '100px',
                dialogImageUrl: '',
                dialogVisible: false,
                fileList: [],
                uploadFiles: [],
                uploadFile: {},
                uploadUrl: '/sys/oss/upload',
                formData: {},
                rules: {
                    roleName: [
                        {required: true, message: '请输入用户名', trigger: 'blur'}
                    ]
                }
            }
        },
        mounted: function () {
        },
        methods: {
            onCancel() {
                this.$refs.ruleForm.resetFields();
                this.$emit('onCancelNewModal');
            },
            onSubmit() {
                this.$refs.ruleForm.validate((valid) => {
                    if (valid) {
                        if (this.$isEmptyStr(this.editId)) {
                            this.submitNewInfo();
                        } else {
                            this.submitEditInfo();
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            handleRemove(file, fileList) {
                this.fileList = fileList;
            },
            handleExceed(file, fileList) {
                this.$message.warn('文件重复');
            },
            handleChange(file, fileList) {
                this.uploadFile = file.raw;
                this.fileList = fileList;
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            uploadRequest(file) {
                // this.subPicForm(param.raw);
                this.uploadFile = file.file;
                this.formData = new FormData();
                this.formData.append('file', this.uploadFile);
            },
            subPicForm() {
                this.$refs.upload.submit();
                this.submitUpload();
                let config = {
                    headers: {
                        'content-type': 'multipart/form-data'
                    }
                }

            },
            async submitUpload(file) {
                let res = await this.$api.post(this.uploadUrl, this.formData);
                if (res.code === 0) {
                } else {
                    this.$message.error(res.msg);
                }
            },
            async getInfo(editId) {
                let res = await this.$api.get('/sys/role/info/' + editId, {});
                if (res.code === 0) {
                    this.newForm.roleName = res.role.roleName;
                    this.newForm.remark = res.role.remark;
                    this.newForm.menuIdList = res.role.menuIdList;
                } else {
                    this.$message.error(res.msg);
                }
            },
            async submitEditInfo() {
                let res = await this.$api.post('/sys/role/update', this.newForm);
                if (res.code === 0) {
                    this.$message.success('保存成功');
                    this.$refs.ruleForm.resetFields();
                    this.$emit('onSubmitNewModal');
                } else {
                    this.$message.error(res.msg);
                }
            },
            async submitNewInfo() {
                let res = await this.$api.post('/sys/role/save', this.newForm);
                if (res.code === 0) {
                    this.$message.success('保存成功');
                    this.$refs.ruleForm.resetFields();
                    this.$emit('onSubmitNewModal');
                } else {
                    this.$message.error(res.msg);
                }
            },
        }
    }
</script>

<style scoped>
    .ele-modal /deep/ .el-form-item__label {
        font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-size: 12px;
        color: #676a6c;
    }

    .ele-modal /deep/ .el-radio__label {
        font-family: "open sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
        font-size: 12px;
        color: #676a6c;
        font-weight: normal;
    }

    .ele-modal /deep/ .el-select {
        display: inline;
    }

    /deep/ .el-upload__input {
        display: none;
    }

    /deep/ .el-dialog__wrapper {
        z-index: 3000;
    }

    .ele-modal /deep/ .el-dialog__header {
        border-bottom: 1px solid #e5e5e5;
        padding: 5px 0 2px 15px !important;
    }

    .ele-modal /deep/ .el-dialog__body {
        padding: 20px;
    }

    .ele-modal /deep/ .el-dialog__footer {
        border-top: 1px solid #e5e5e5;
        padding: 15px;
    }
</style>
